import { ref, computed } from 'vue';
import {defineStore} from 'pinia'
import {Login,GetUser,GetShop} from '@/api'
import { useRouter } from 'vue-router';
export const useMainStore = defineStore("main",()=>{
    const num = ref(0);
    const token = ref('');
    const userInfo = ref({});
    const userMore = ref({})
    const doubleCount = computed(()=>num.value*2)
    const $router = useRouter();
    const shop = ref({})
    function addNum(n=1){
        console.log(num.value);
        num.value+=n
    }
    function getShop(data:any){
        GetShop(data)
        .then(res=>{
            shop.value = res.data.data[0];
        })
    }
    function getUser(params:any){
        GetUser(params)
        .then(res=>{
            userMore.value = res.data.data[0]
            getShop({id:res.data.data[0].shop});
        })
    }
    function login(data:any){
        Login(data)
        .then(res=>{
            console.log(res,"res");
            userInfo.value = res.data.user;
            token.value = res.data.token;
             console.log(res.data.user);
            localStorage.setItem("userInfo",JSON.stringify(res.data.user));
            localStorage.setItem("token",res.data.token)
            $router.push("/about");
            getUser({...res.data.user});

        })
        .catch(err=>{
            console.log(err,"err");
        })
    }
     
    return {num,doubleCount,addNum,login,userMore,userInfo,shop,getUser}
})